<?php defined('SYSPATH') or die('No direct script access.');

// Категории
class Controller_Categories extends Controller_Base
{
    protected $per_page = 20;
    
	public function action_get_category()
	{
		$category_id = $this->request->param('category_id');
        
        $category = $this->category_model->get_category($category_id);
        $category['url'] = $this->main_model->transliterate($category['name']);
        
        $menu = $this->category_model->get_categories($category_id);
        $this->main_model->transliterate_array($menu, 'name');
        
        $breadcrumbs = $this->category_model->get_breadcrumbs($category_id);
        $this->main_model->transliterate_array($breadcrumbs, 'name');
        
        $content = null;
        $books = array();
        if(!count($menu))   // Категория не имеет вложенных
        {
            $page = $this->request->param('page') ? $this->request->param('page') : 1;
            
            $count  = $this->per_page;
            $offset = ($page - 1)*$count;
            
            $pagination = array(
                'total'    => $this->book_model->get_count_books($category_id),
                'current'  => $page,
                'per_page' => $count,
                'url'      => '/'.$category_id.'-'.$category['url'] 
            );
            
            $books = $this->book_model->get_books($category_id, $count, $offset);
            $this->main_model->transliterate_array($books, 'name'); 
              
            $content = new View('category_top_tpl');
            $content->pagination = $pagination;             
        }
        else
        {
            $category_ids = $this->category_model->get_child_categories($category_id); 
            $books = $this->book_model->get_popular_books($category_ids, 12);
            $this->main_model->transliterate_array($books, 'name');
            
            $content = new View('category_tpl');
            $this->slider = true;  
        } 
        
        $title   = $category['name'];
        
        $content->category    = $category;
        $content->breadcrumbs = $breadcrumbs;
        $content->books       = $books;
                
        $this->print_template($title, $content, $menu);
	}
    
} // End Categories
